<template>
  <div v-loading="loading" class="exam-result-wrapper">
    <div class="score" :class="{ blue: isPass }">
      <span>{{ splitScore.int }}</span>
      <span class="sm">.{{ splitScore.float }}</span>
    </div>
    <div class="text">{{ resText }}</div>
    <div class="info-wrapper">
      <div v-for="item in infoColumns" :key="item.key" class="info">
        <span class="label">{{ item.label }}:</span>
        <span class="value">{{ infoData[item.key] }}</span>
      </div>
    </div>
    <div class="chart-wrapper">
      <div ref="chart" style="width: 100%; height: 400px"></div>
    </div>
    <el-button @click="onResetExam">重新测评</el-button>
  </div>
</template>

<script>
import { consequence } from "@/api";
import * as echarts from "echarts";

export default {
  name: "ExamResult",
  data() {
    return {
      loading: false,
      isPass: true,
      infoColumns: [
        {
          key: "totalTime",
          label: "练习用时",
        },
        {
          key: "examTime",
          label: "练习时间",
        },
        {
          key: "score",
          label: "实操得分",
        },
      ],
      infoData: {},
      chartData: [],
    };
  },
  computed: {
    isPractice() {
      const examType = JSON.parse(localStorage.getItem("examType"));
      return examType === "2";
    },
    resText() {
      const text = this.isPractice ? "练习" : "考试";
      return this.isPass
        ? `恭喜您，本次${text}内容通过!`
        : `很遗憾，本次${text}内容未通过!`;
    },
    splitScore() {
      const [int = 0, float = 0] = (this.infoData.score || "").split(".");
      return { int, float };
    },
  },
  methods: {
    async initData() {
      this.loading = true;
      const res = await consequence({ uuid: this.$store.state.user.userId });

      const {
        stepCompleteScroce,
        operateCorrectScroce,
        resultStatusScroce,
        operateTimeScroce,
        date,
        startDate,
        scroce,
      } = res;
      this.chartData = [
        stepCompleteScroce,
        operateCorrectScroce,
        resultStatusScroce,
        operateTimeScroce,
      ];
      this.infoData = {
        totalTime: date,
        examTime: startDate,
        score: scroce,
      };
      this.isPass = Number(scroce) >= 60;
      this.loading = false;
    },
    initChart() {
      const chartContainer = this.$refs.chart;
      const myChart = echarts.init(chartContainer);

      const options = {
        title: {
          text: "实操对练",
          top: 0,
          left: "center",
          textStyle: {
            color: "#5470c6",
          },
        },
        radar: {
          center: ["50%", "50%"],
          radius: "60%",
          indicator: [
            { name: "路径完整", max: 100 },
            { name: "操作准确", max: 100 },
            { name: "结果状态", max: 100 },
            { name: "操作时长", max: 100 },
          ],
        },
        series: [
          {
            type: "radar",
            data: [
              {
                value: this.chartData,
              },
            ],
          },
        ],
      };

      myChart.setOption(options);
    },
    onResetExam() {
      this.$router.push("/exam");
    },
  },
  async mounted() {
    await this.initData();
    this.initChart();
  },
};
</script>

<style lang="scss" scoped>
.exam-result-wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 30px;
  overflow-y: auto;
  height: 100%;

  .score {
    width: 80px;
    height: 80px;
    line-height: 80px;
    border-radius: 50%;
    background: rgb(245, 108, 108);
    font-size: 24px;
    font-weight: 500;
    color: #fff;
    text-align: center;
    box-shadow: 0 2px 12px rgba(245, 108, 108, 0.302);

    &.blue {
      background: #4080ff;
      box-shadow: 0 2px 12px rgba(82, 139, 255, 0.302);
    }

    .sm {
      font-size: 14px;
    }
  }

  .text {
    font-size: 22px;
    font-weight: 500;
    margin: 20px 0 30px;
  }

  .info-wrapper {
    display: flex;
    flex-wrap: wrap;
    width: 50%;
    text-align: center;

    .info {
      flex: 1;
      font-size: 14px;

      .label {
        margin-right: 8px;
        color: #909399;
      }
    }
  }

  .chart-wrapper {
    width: 100%;
    height: 400px;
    margin: 30px 0;
  }
}
</style>
